package name.caiyao.fakegps.hook;

import java.io.File;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

//http://api.cellocation.com/recell/?lat=39.5324&lon=116.3424&n=1&mnc=0

public class LocationHook implements IXposedHookLoadPackage {

	@Override
	public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {

		String packageName = loadPackageParam.packageName;
		if (!"com.tencent.mm".equals(packageName)) {
			return;
		}

		Location location = getLocation();
		if (location != null) {
			double longitude = location.getLon();// 经度
			double latitude = location.getLat();// 纬度
			int mcc = 460;
			int mnc = location.getMnc();
			int lac = location.getLac();// 位置区域码
			int cid = location.getCid();// 基站编号

			XposedBridge.log("模拟定位:" + location.toString());
			HookUtils.HookAndChange(loadPackageParam.classLoader, latitude, longitude, mcc, mnc, lac, cid);
		}
	}

	/**
	 * 从/sdcard/weixin_location.properties文件中读取配置信息
	 * 
	 * @return
	 */
	private Location getLocation() {
		String filePath = ToolProperties.getSDCardPath() + "/weixin_location.properties";
		File file = new File(filePath);
		if (!file.exists()) {
			return null;
		}

		String lat = ToolProperties.readProp(filePath, "lat", "0.0");
		String lon = ToolProperties.readProp(filePath, "lon", "0.0");
		String mnc = ToolProperties.readProp(filePath, "mnc", "0");
		String lac = ToolProperties.readProp(filePath, "lac", "0");
		String cid = ToolProperties.readProp(filePath, "cid", "0");

		Location location = new Location(Double.parseDouble(lon), Double.parseDouble(lat), 460, Integer.parseInt(mnc),
				Integer.parseInt(lac), Integer.parseInt(cid));

		return location;
	}
}
